package com.woniuxy.platform.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.domain.CertFields;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.platform.dao.PlatformAccount;
import com.woniuxy.platform.feign.CertifiedClient;
import com.woniuxy.platform.model.dto.PlatformLoginDto;
import com.woniuxy.platform.model.form.AddpaltformForm;
import com.woniuxy.platform.model.form.LoadAllpaltforminfoForm;
import com.woniuxy.platform.model.param.LoadAllpaltforminfoParam;
import com.woniuxy.platform.service.PaltformAccountRoleService;
import com.woniuxy.platform.service.PlatformAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 *
 */

@RestController
@Api(
        tags = {"平台相关接口"}
)
@RequestMapping("/platformAccount")
public class PlatformAccountController {
    @Autowired
    PlatformAccountService platformAccountService;

    @Autowired
    CertifiedClient certifiedClient;

    @ApiOperation("测试")
    @GetMapping("/test")
    public Result<String> test(){
        platformAccountService.test();
        return Result.getResult(String.class).setMessage("测试成功");
    }


    @ApiOperation("平台账号注册申请")
    @PostMapping({"/paltformregister"})
    private Result addpaltform(@RequestBody AddpaltformForm form)  {
        this.platformAccountService.paltformregister(form);
        return Result.getResult().setMessage("注册成功，待管理员审核");
    }

    @ApiOperation("平台账号登录")
    @PostMapping({"/paltformlogin"})
    private Result paltformlogin(@RequestBody AddpaltformForm form, HttpServletResponse response) {
        PlatformLoginDto platformLoginDto = this.platformAccountService.paltformlogin(form);
        System.out.println("------------"+platformLoginDto.toString());
        HashMap<String, Object> map = new HashMap();
        map.put("id", platformLoginDto.getPlatformId());
        String token = JwtUtil.creatUserToken(map);
        response.setHeader("token", token);
        System.out.println(token);
        response.setHeader("Access-Control-Expose-Headers", "token");
        return Result.getResult().setMessage("平台账户密码登录成功");
    }


    @ApiOperation("平台管理员修改待审核管理员状态")
    @GetMapping("/updateplatformState")
    @ApiImplicitParam(name = "platformId",value = "平台id")
    public Result updateplatformState(@RequestHeader String token,Integer platformId){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        PlatformAccount platformAccount= platformAccountService.updateplatformState(Integer.valueOf(id),platformId);
        return Result.getResult(PlatformAccount.class).setMessage("修改待审核管理员状态成功").setData(platformAccount);
    }

    @ApiOperation("平台管理员拒绝待审核管理员")
    @GetMapping("/delplatformState")
    @ApiImplicitParam(name = "platformId",value = "平台id")
    public Result delplatformState(@RequestHeader String token,Integer platformId){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        PlatformAccount platformAccount= platformAccountService.delplatformState(Integer.valueOf(id),platformId);
        return Result.getResult(PlatformAccount.class).setMessage("拒绝待审核管理员成功").setData(platformAccount);
    }

    @ApiOperation("通过账号查找待审核管理员")
    @GetMapping("/LoadAdminByPlatformAccount")
    @ApiImplicitParam(name = "platformAccounts",value = "平台账号")
    public Result LoadAdminByPlatformAccount(String platformAccounts){
        PlatformAccount platformAccount= platformAccountService.LoadAdminByPlatformAccount(platformAccounts);
        return Result.getResult(PlatformAccount.class).setMessage("通过账号查找待审核管理员成功").setData(platformAccount);
    }


    @ApiOperation("平台管理员查询待审核管理员列表")
    @GetMapping("/LoadALLplatform")
    public Result<PageInfo> LoadALLplatform(@RequestHeader String token, LoadAllpaltforminfoForm laf){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        LoadAllpaltforminfoParam param = BeanUtil.toBean(laf, LoadAllpaltforminfoParam.class);
        PageInfo<PlatformAccount> pageInfo =platformAccountService.LoadALLplatform(param,id);
        return Result.getResult(PageInfo.class).setMessage("平台管理员查询待审核管理员列表成功").setData(pageInfo);
    }

    @ApiOperation("平台管理员查询全部管理员列表")
    @GetMapping("/LoadALLplatforms")
    public Result<PageInfo> LoadALLplatforms(@RequestHeader String token, LoadAllpaltforminfoForm laf){
        String id = JwtUtil.parseUserToken(token).get("id").toString();
        System.out.println(id);
        LoadAllpaltforminfoParam param = BeanUtil.toBean(laf, LoadAllpaltforminfoParam.class);
        PageInfo<PlatformAccount> pageInfo =platformAccountService.LoadALLplatforms(param,id);
        return Result.getResult(PageInfo.class).setMessage("平台管理员查询待审核管理员列表成功").setData(pageInfo);
    }





}

